﻿/**
 * Licensed under the MIT License
 *
 * Copyright (c) 2010 Laurent Berthelot
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */
package marcel.display.draw.shapes
{
	import flash.display.Graphics;
	import flash.geom.Rectangle;
	import marcel.display.draw.props.FillProps;
	import marcel.display.draw.props.LineProps;
	import marcel.display.draw.props.PolygonProps;
	
	
	/**
	 * The PolygonShape class contains a static method that you can use to draw a polygon shape.
	 * @author	Laurent Berthelot
	 */
	public class PolygonShape extends BasicShape
	{
		//--------------------------------------------------------------------------
		//
		//  Constructor
		//
		//--------------------------------------------------------------------------
		/**
		 * Non-instanciable class, only statics methods in PolygonShape class.
		 */
		public function PolygonShape(o:ConstructorAccess) {}

		//--------------------------------------------------------------------------
		//
		//  Public static methods
		//
		//--------------------------------------------------------------------------
		/**
		 * Draws a Polygon
		 * @param	graphics	Graphics object to draw on.
		 * @param	pp			Polygon properties.
		 * @param	close		Specify if the shape is closed (true) or not (false).
		 * @param	fp			Fill properties.
		 * @param	lp			Line style properties.
		*/
		public static function drawShape(graphics:Graphics, pp:PolygonProps = null, close:Boolean = true, fp:FillProps = null, lp:LineProps = null):Graphics {
			_lineStyle(graphics, lp);
			var _points:/*Point*/Array = pp.points;
			var xMin:Number = _points.concat().sortOn("x", Array.NUMERIC)[0].x;
			var xMax:Number = _points.concat().sortOn("x", Array.NUMERIC | Array.DESCENDING)[0].x;
			var yMin:Number = _points.concat().sortOn("y", Array.NUMERIC)[0].y;
			var yMax:Number = _points.concat().sortOn("y", Array.NUMERIC | Array.DESCENDING)[0].y;
			_beginFill(graphics, fp, new Rectangle(xMin, yMin, xMax - xMin, yMax - yMin));
			graphics.moveTo(_points[0].x, _points[0].y);
			var _nbPoint:int = _points.length;
			for (var i:int = 1; i < _nbPoint; i++) graphics.lineTo(_points[i].x, _points[i].y);
			if (close && !_points[0].equals(_points[_nbPoint - 1])) graphics.lineTo(_points[0].x, _points[0].y);
			_endFill(graphics);
			return graphics;
		}
	}
}
internal class ConstructorAccess {}